package mortal.game.control
{
    import Message.BroadCast.*;
    import fl.data.*;
    import flash.events.*;
    import flash.utils.*;
    import modules.*;
    import mortal.game.events.*;
    import mortal.game.mvc.*;
    import mortal.game.scene.layer.utils.*;
    import mortal.game.scene.player.*;
    import mortal.mvc.core.*;
    import mortal.mvc.interfaces.*;

    public class NearbyListController extends Controller
    {
        private var _nearbyListModule:NearbyListModule;

        public function NearbyListController()
        {
            
        }

        override protected function initServer() : void
        {
            Dispatcher.addEventListener(EventName.ChangeScene, this.onChangeSceneHandler);
            
        }

        override protected function initView() : IView
        {
            if (!this._nearbyListModule)
            {
                this._nearbyListModule = new NearbyListModule();
                this._nearbyListModule.addEventListener(Event.ADDED_TO_STAGE, this.onAddToStage);
                this._nearbyListModule.addEventListener(NearbyListModule.Event_Refresh_PlayerList, this.onNearbyPlayerListRefresh);
                this._nearbyListModule.addEventListener(NearbyListModule.Event_Refresh_TeamList, this.onNearbyTeamListRefresh);
            }
            return this._nearbyListModule;
        }

        private function onAddToStage(event:Event) : void
        {
            this.doRefresh();
            
        }

        private function doRefresh() : void
        {
            if (this._nearbyListModule)
            {
                switch(this._nearbyListModule.currentTabbarIndex)
                {
                    case 0:
                    {
                        this.doRefreshPlayerList();
                        break;
                    }
                    case 1:
                    {
                        this.doRefreshNpcList();
                        break;
                    }
                    case 2:
                    {
                        this.doRefreshTeamList();
                        break;
                    }
                    default:
                    {
                        break;
                    }
                }
            }
            
        }

        private function onNearbyPlayerListRefresh(event:DataEvent) : void
        {
            this.doRefreshPlayerList();
            
        }

        private function onNearbyTeamListRefresh(event:DataEvent) : void
        {
            this.doRefreshTeamList();
            
        }

        public function showNearbyPanleWithIndex(param1:int) : void
        {
            view.show();
            this._nearbyListModule.updateCurrentStatus(param1);
            
        }

        private function onChangeSceneHandler(event:DataEvent) : void
        {
            this.doRefreshNpcList();
            
        }

        private function doRefreshPlayerList() : void
        {
            var _loc_4:IEntity = null;
            var _loc_1:* = ThingUtil.entityUtil.getAllUserList();
            var _loc_2:Array = [];
            var _loc_3:int = 0;
            while (_loc_3 < _loc_1.length)
            {
                
                _loc_4 = _loc_1[_loc_3] as IEntity;
                if (_loc_4.entityInfo.entityInfo.entityId.id != cache.role.entityInfo.entityId.id)
                {
                    _loc_2.push({label:"", player:_loc_4.entityInfo.entityInfo});
                }
                _loc_3++;
            }
            this._nearbyListModule.updatePlayerList(new DataProvider(_loc_2.slice()));
            
        }

        private function doRefreshNpcList() : void
        {
            if (this._nearbyListModule)
            {
                this._nearbyListModule.updateNpcList();
            }
            
        }

        private function doRefreshTeamList() : void
        {
            var _loc_5:SEntityInfo = null;
            var _loc_6:SMiniGroupInfo = null;
            var _loc_7:String = null;
            var _loc_9:Object = null;
            var _loc_1:* = ThingUtil.entityUtil.getAllUserList();
            var _loc_2:Array = [];
            var _loc_3:Array = [];
            var _loc_4:* = new Dictionary();
            var _loc_8:int = 0;
            while (_loc_8 < _loc_1.length)
            {
                
                _loc_5 = (_loc_1[_loc_8] as IEntity).entityInfo.entityInfo;
                if (_loc_5.entityId.id != cache.role.entityInfo.entityId.id)
                {
                    if (!_loc_5.groupId || _loc_5.groupId && _loc_5.groupId.length == 0)
                    {
                        _loc_3.push({label:_loc_5.name, player:_loc_5});
                    }
                    else if (_loc_5.groupId)
                    {
                        _loc_6 = _loc_5.groupId[0] as SMiniGroupInfo;
                        _loc_7 = EntityUtil.toString(_loc_6.groupID);
                        if (!_loc_4[_loc_7] && _loc_6.maxPlayerCount != _loc_6.playerCount)
                        {
                            _loc_4[_loc_7] = {label:_loc_7, group:_loc_6, player:_loc_5};
                        }
                    }
                }
                _loc_8++;
            }
            for each (_loc_9 in _loc_4)
            {
                
                _loc_2.push(_loc_9);
            }
            this._nearbyListModule.updateTeamList(_loc_2.slice(), _loc_3.slice());
            
        }

    }
}
